<?php


namespace App\Http\Controllers\Api\Expenses;


use App\Http\Controllers\Controller;
use App\Http\Requests\TariffRequest;
use App\Repositories\TariffRepository;
use App\Services\TariffService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class TariffController extends Controller
{


    /**
     * 分页列表
     * @param Request $request
     * @param TariffRepository $repository
     * @return TariffController|JsonResponse
     * @Author 姿势就是力量
     */
    public function getPageList(Request $request, TariffRepository $repository)
    {
        $companyId = (int)$request->get('company_id', '');  //公司id
        $type = (string)$request->get('type', '1');         //分类：1-乘客，2-公司
        $pageSize = (int)$request->get('page_size', 10);
        $name = (string)$request->get('name', '');        //资费名称
        $service_type = (int)$request->get('service_id', '');        //业务类型
        $creator_name = (string)$request->get('creator_name', '');        //创建人
        $modifier_name = (string)$request->get('modifier_name', '');        //修改人
        $date = (string)$request->get('created_at','');                          //日期选择
        $updated_at = (string)$request->get('updated_at','');   //修改时间
        $columns = ['id', 'name', 'created_at','service_type_id', 'company_id', 'created_at', 'updated_at', 'creator_id', 'modifier_id'];
        $list = $repository->getPageList($companyId,$service_type,$name,$creator_name,$modifier_name,$type, $date,$updated_at,$pageSize,$columns);

        return $this->sendJson($list);
    }


    /**
     * 资费详情
     * @param Request $request
     * @param TariffRepository $repository
     * @return TariffController|JsonResponse
     * @Author 姿势就是力量
     */
    public function getFirst(Request $request, TariffRepository $repository)
    {
        $id = (int)$request->get('id', '');
        $info = $repository->find($id);

        return $this->sendJson($info);
    }


    /**
     * 保存信息
     * @param TariffRequest $request
     * @param TariffRepository $repository
     * @return TariffController|JsonResponse
     * @Author 姿势就是力量
     */
    public function save(TariffRequest $request, TariffRepository $repository)
    {
        $data = $request->post();
        if($request->post('rules')==0)
        {
            $data['starting_price'] = $request->post('starting_price',[]);
            if(empty($request->post('basics'))) return  $this->sendError('基础费必传');
        }else{
            $data['basics'] = $request->post('basics',[]);
            if(empty($request->post('starting_price'))) return  $this->sendError('起步费必传');
        }

        $tariff = $repository->saveData($data);
        if (!$tariff) return $this->sendError('保存失败');

        return $this->sendSuccess('保存成功');
    }


    /**
     * 删除
     * @param Request $request
     * @param TariffService $service
     * @return TariffController|JsonResponse
     * @Author 姿势就是力量
     */
    public function delete(Request $request, TariffService $service)
    {
        $id = (int)$request->post('id', '');
        $res = $service->delete($id);
        if (!$res) return $this->sendError($service->getError());

        return $this->sendSuccess('删除成功');
    }

}
